package lendingbooks.activities;

import java.util.ArrayList;
import java.util.Collection;

import lendingbooks.copy.Copy;
import lendingbooks.copy.CopyListAdapter;
import lendingbooks.copy.CopyListViewRow;
import lendingbooks.copy.CopyManager;
import lendingbooks.copy.FriendCopyListAdapter;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;

public class MyFriendsLibraryActivity extends Activity {
	
	private Collection<Copy> myFriendsLibraryBooks = null;
	private ListView listViewMyFriendsLibrary = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_friends_library);
		Log.d("LENDINGBOOKS MyFriendsLibraryActivity","_onCreate_");
		listViewMyFriendsLibrary = (ListView) findViewById(R.id.listViewMyFriendsLibrary);
		refreshMyFriendsLibraryBookList();
		Log.d("LENDINGBOOKS MyFriendsLibraryActivity", "_"+(listViewMyFriendsLibrary==null));
		((PullToRefreshListView) listViewMyFriendsLibrary).setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				refreshMyFriendsLibraryBookList();
			}
		});
	}

	public void refreshMyFriendsLibraryBookList() {
		final ProgressDialog dialog = ProgressDialog.show(MyFriendsLibraryActivity.this, "", "Loading. Please wait...", true);
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				ArrayList<CopyListViewRow> myFriendsLibraryBooksListViewRow = new ArrayList<CopyListViewRow>();
				for(Copy copy:myFriendsLibraryBooks){
					myFriendsLibraryBooksListViewRow.add(new CopyListViewRow(copy));
				}
				FriendCopyListAdapter friendCopyListAdapter = new FriendCopyListAdapter(listViewMyFriendsLibrary.getContext(),
						R.layout.list_copy_row, myFriendsLibraryBooksListViewRow);
				listViewMyFriendsLibrary.setAdapter(friendCopyListAdapter);
				friendCopyListAdapter.notifyDataSetChanged();
				listViewMyFriendsLibrary.setOnItemClickListener(new OnItemClickListener(){
					@Override
					public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
						/**
						CopyListViewRow copyListViewRow = (CopyListViewRow) parent.getItemAtPosition(position);
						if(copyListViewRow.getIntent()==null){
							copyListViewRow.setIntent(new Intent(getApplicationContext(), CopyActivity.class));
						}
						copyListViewRow.getIntent().putExtra("copy",copyListViewRow.getCopy() );
						startActivity(copyListViewRow.getIntent());
						**/
					}
				});	
				((PullToRefreshListView) listViewMyFriendsLibrary).onRefreshComplete();
				dialog.dismiss();
			}
		};

		Thread loadLibrary = new Thread() {
			@Override
			public void run() {
				CopyManager cm = new CopyManager();
				myFriendsLibraryBooks = cm.getMyFriendsLibrary();
				Message message = new Message();
				handler.sendMessage(message);
			}
		};
		loadLibrary.start();
	}
	
}
